<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="queryParams" ref="queryParamsRef" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="收费科室id" prop="chargeDeptId">
          <el-input v-model="queryParams.chargeDeptId" placeholder="请输入收费科室id" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="科室id" prop="deptId">
          <el-input v-model="queryParams.deptId" placeholder="请输入科室id" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
        </el-form-item>
      </el-form>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleAdd" v-hasPerm="['business:ChargeVsDept:add']">
          <template #icon>
            <i-ep-plus />
          </template>
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain :disabled="single" @click="handleUpdate"
          v-hasPerm="['business:ChargeVsDept:edit']">
          <template #icon>
            <i-ep-edit />
          </template>
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain :disabled="multiple" @click="handleDelete"
          v-hasPerm="['business:ChargeVsDept:remove']">
          <template #icon>
            <i-ep-delete />
          </template>
          删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-adaptive :data="ChargeVsDeptList" :border="true" :stripe="true" highlight-current-row
       :header-cell-style="$headerCellStyle" >
      <el-table-column label="收费科室id" :align="'center'" prop="chargeDeptId" />
      <el-table-column label="科室id" :align="'center'" prop="deptId" />
      <el-table-column label="机构id" :align="'center'" prop="orgId" />
      <el-table-column label="操作" :align="'center'" class-name="small-padding fixed-width" width="220">
        <template #default="scope">
          <el-button type="success" @click="handleUpdate(scope.row)" v-hasPerm="['business:ChargeVsDept:edit']">
            <template #icon>
              <i-ep-edit />
            </template>
            修改
          </el-button>
          <el-button type="danger" @click="handleDelete(scope.row)" v-hasPerm="['business:ChargeVsDept:remove']">
            <template #icon>
              <i-ep-delete />
            </template>
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改收费科室对照对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="收费科室id" prop="chargeDeptId">
          <el-input v-model="form.chargeDeptId" placeholder="请输入收费科室id" />
        </el-form-item>
        <el-form-item label="科室id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入科室id" />
        </el-form-item>
        <el-form-item label="机构id" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入机构id" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
export default {
  name: "ChargeVsDept"
}
</script>

<script setup lang="ts">
import {
  addChargeVsDept,
  delChargeVsDept,
  getChargeVsDept,
  listChargeVsDept,
  updateChargeVsDept,
  selectPageChargeVsDept
} from "@/api/chargeVs/chargeVsDept";

import {useUserStoreHook} from "@/store/modules/user"

const useUserStore = useUserStoreHook();
const { proxy } = getCurrentInstance() as any;
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo

const formRef = ref();
const queryParamsRef = ref()
const ChargeVsDeptList = ref([]);
const open = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const  form = ref<any>({
  chargeDeptId: "",
  deptId: "",
  orgId: ""
})
const rules = ref<any>({
    chargeDeptId: [
      {
        required: true, message: "收费科室id不能为空", trigger: "blur"
      }
    ],
    deptId: [
      {
        required: true, message: "科室id不能为空", trigger: "blur"
      }
    ]
})

const queryParams = ref<any>({
  pageNum: 1,
  pageSize: 10,
  chargeDeptId: "",
  deptId: "",
  orgId: "",
})

if (userInfo.orgId) {
  queryParams.value.orgId = userInfo.orgId
  getList()
}
/** 查询收费科室对照列表 */
function getList() {
  selectPageChargeVsDept(queryParams.value).then((response: any) => {
    ChargeVsDeptList.value = response.rows;
    total.value = response.total;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    chargeDeptId: "",
    deptId: "",
    orgId: userInfo.orgId
  };
  nextTick(() => {
    formRef.value.resetFields()
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParamsRef.value.resetFields()
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  if (!queryParams.value.orgId) {
    return ElMessage.error("请先选择机构")
  }
  open.value = true;
  reset();
  title.value = "添加收费科室对照";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  open.value = true;
  reset();
  const _id = row.chargeVsId || ids.value
  getChargeVsDept(_id).then((response: any) => {
    form.value = response.data;
    title.value = "修改收费科室对照";
  });
}

/** 提交按钮 */
function submitForm() {
  (formRef.value as any).validate(valid => {
    if (valid) {
      if (form.value.chargeVsId != null) {
        updateChargeVsDept(form.value).then((response: any) => {
          ElMessage.success("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      } else {
        addChargeVsDept(form.value).then((response: any) => {
          ElMessage.success("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.chargeVsId ||
    ids.value;
  ElMessageBox.confirm('是否确认删除收费科室对照编号为"' + ids + '"的数据项？',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    delChargeVsDept(_ids).then(() => {
      getList();
      ElMessage.success("删除成功");
    })
  }).catch(() => {
  })
}

</script>
